<?php

class Forum_PostController extends Zend_Controller_Action
{

  public function init()
  {
    /* Initialize action controller here */
  }

  public function indexAction()
  {
    // action body
  }

  public function deleteAction()
  {
    $post_id = $this->_getParam('post_id');
    if ($post_id) {
      $post = new Forum_Model_Post($post_id);
      if (!$post) {
        $this->view->error = 'Такого поста нету !!!';
        return;
      }
    } else {
      $this->view->error = 'Не задан ID поста !!!';
      return;
    }
    $viewer = Engine_Api::_()->application()->getViewer();
    if ($viewer->user_id != $post->user_id && !Engine_Api::_()->application()->isAdmin()) {
      $this->view->error = 'Нет доступа для удалить этот пост';
      return;
    }
    $this->view->message = 'Вы действительно хотите удалить это сообщение ?';
    $this->view->form = $form = new Forum_Form_Post_Delete();

    if ($this->getRequest()->isPost()) {
      $data = $this->getRequest()->getPost();
      if ($form->isValid($data)) {
        if (isset($data['submit'])) {
          $post->delete();
        }
        $this->redirect('forum/topic/view');
      }
    }
  }

  public function editAction()
  {
    $post_id = $this->_getParam('post_id');
    if ($post_id) {
      $post = new Forum_Model_Post($post_id);
      if (!$post) {
        $this->view->error = 'Такого поста нету !!!';
        return;
      }
    } else {
      $this->view->error = 'Не задан ID поста !!!';
      return;
    }
    $viewer = Engine_Api::_()->application()->getViewer();
    if ($viewer->user_id != $post->user_id && !Engine_Api::_()->application()->isAdmin()) {
      $this->view->error = 'Нет доступа для редактирование этого поста';
      return;
    }

    $this->view->form = $form = new Forum_Form_Post_Create();

    if ($this->getRequest()->isPost()) {
      $data = $this->getRequest()->getPost();
      if ($form->isValid($data)) {
        $post->fill($data);
        $post->save();
        $this->redirect('forum/topic/view');
      }
    }
    $form->populate(array('body' => $post->body));
  }

  public function quoteAction()
  {
    $this->view->viewer = $viewer = Engine_Api::_()->application()->getViewer();
    $post_id = $this->_getParam('post_id');
    if ($post_id) {
      $post = new Forum_Model_Post($post_id);
      if (!$post) {
        $this->view->error = 'Такого поста нету !!!';
        return;
      }
    } else {
      $this->view->error = 'Не задан ID поста !!!';
      return;
    }

    $this->view->form = $form = new Forum_Form_Post_Create();

    if ($this->getRequest()->isPost()) {
      $data = $this->getRequest()->getPost();
      if ($form->isValid($data)) {
        $quote = new Forum_Model_Post();
        $quote->fill($data);
        $quote->setUser($viewer);
        $quote->topic_id = 1;
        $quote->forum_id = 1;
        $quote->creation_date = date("Y-m-d H:i:s");
        $quote->save();
        $this->redirect('forum/topic/view');
      }
    }
    $user = new Application_Model_User($post->user_id);
    $body = "<blockquote><strong>" . $user->getDisplayName() . "</strong> " . $post->creation_date . "<br />" . $post->body . "</blockquote><br />";
    $form->populate(array('body' => $body));
  }
}






